package com.example.servlet.flight;

import com.example.entity.City;
import com.example.entity.FlightDetail;
import com.example.entity.Manager;
import com.example.entity.User;
import com.example.service.FlightService;
import com.example.service.impl.FlightServiceImpl;
import com.example.utils.MybatisUtil;
import com.example.utils.ThymeleafUtil;
import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.session.SqlSession;
import org.thymeleaf.context.Context;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebServlet("/manager_flight_search2_2")
public class mFlightSearch22Servlet extends HttpServlet {
    private FlightService service;
    private SqlSession sqlSession;
    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        sqlSession = MybatisUtil.getSession();
        service = new FlightServiceImpl(sqlSession);
    }
    @Override
    public void destroy() {
        sqlSession.close();
        super.destroy();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Manager manager = (Manager) req.getSession().getAttribute("manager");
        String roundDepartureCity = req.getParameter("roundDepartureCity");
        String roundArrivalCity = req.getParameter("roundArrivalCity");
        String roundDepartureDateStr = req.getParameter("roundDepartureDate");
        String roundReturnDateStr = req.getParameter("roundReturnDate");
        Date roundReturnDate = null;
        try {
            if(roundReturnDateStr != null){
                roundReturnDate = new SimpleDateFormat("yyyy-MM-dd").parse(roundReturnDateStr);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Context context = new Context();
        if(roundDepartureCity.equals(roundArrivalCity)){
            context.setVariable("city_same", true);
        }else{
            List<FlightDetail> flightDetails2 = new ArrayList<>();
            if (service.isCityExist(roundDepartureCity) == 0 && service.isCityExist(roundArrivalCity) == 0) {
                flightDetails2.addAll(service.searchFlightDetails(roundArrivalCity, roundDepartureCity, roundReturnDate));
            } else if (service.isCityExist(roundDepartureCity) == 0 && service.isCityExist(roundArrivalCity) !=0 ) {
                context.setVariable("no_round_arrival", true);
                City city = service.getNearCity(roundArrivalCity);
                String roundArrivalCity1 = city.getCity1();
                String roundArrivalCity2 = city.getCity2();
                flightDetails2.addAll(service.searchFlightDetails(roundArrivalCity1, roundDepartureCity, roundReturnDate));
                flightDetails2.addAll(service.searchFlightDetails(roundArrivalCity2, roundDepartureCity, roundReturnDate));
            } else if (service.isCityExist(roundDepartureCity) != 0 && service.isCityExist(roundArrivalCity) == 0) {
                context.setVariable("no_round_departure", true);
                City city = service.getNearCity(roundDepartureCity);
                String roundDepartureCity1 = city.getCity1();
                String roundDepartureCity2 = city.getCity2();
                flightDetails2.addAll(service.searchFlightDetails(roundArrivalCity, roundDepartureCity1, roundReturnDate));
                flightDetails2.addAll(service.searchFlightDetails(roundArrivalCity, roundDepartureCity2, roundReturnDate));
            } else {
                context.setVariable("no_departure_arrival", true);
                City city1 = service.getNearCity(roundDepartureCity);
                City city2 = service.getNearCity(roundArrivalCity);
                flightDetails2.addAll(service.searchFlightDetails(city2.getCity1(), city1.getCity1(), roundReturnDate));
                flightDetails2.addAll(service.searchFlightDetails(city2.getCity2(), city1.getCity1(), roundReturnDate));
                flightDetails2.addAll(service.searchFlightDetails(city2.getCity1(), city1.getCity2(), roundReturnDate));
                flightDetails2.addAll(service.searchFlightDetails(city2.getCity2(), city1.getCity2(), roundReturnDate));
            }
            context.setVariable("managername", manager.getM_name());
            context.setVariable("flightdetails2", flightDetails2);
            context.setVariable("roundDepartureCity", roundDepartureCity);
            context.setVariable("roundArrivalCity", roundArrivalCity);
            context.setVariable("roundDepartureDate", roundDepartureDateStr);
            context.setVariable("roundReturnDate", roundReturnDateStr);
        }
        ThymeleafUtil.process("manager_flight_result2_2.html", context, resp.getWriter());
    }
}
